package com.diallom.parkingnord;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.diallom.R;
import com.diallom.bd.Parking;
import com.diallom.bd.ParkingDAO;
import com.diallom.bd.ParkingDB;

public class ListParking extends ListActivity {
	private  ParkingDB db;
	private RatingBar rating;
	private  ArrayList<Parking> list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.parkings);
		db = new ParkingDB(this);
		list = db.getParkingsToList();
	}
	@Override
	protected void onStart(){
		super.onStart();
		//displayListParking();
		
		setListAdapter(new ParkingAdapter(this));
	}
	
	class ParkingAdapter extends ArrayAdapter<Parking>{

		public ParkingAdapter(Context context) {
			super(context, R.layout.parking,list);
			// TODO Auto-generated constructor stub
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View ligne = convertView;
			if (ligne == null) {
				LayoutInflater xml = ListParking.this.getLayoutInflater();
				ligne = xml.inflate(R.layout.parking, null);
			}

			// en fonction de la position de la ligne dans la liste, on affiche
			// des contenus différents
			TextView nom_parking = (TextView) ligne.findViewById(R.id.nom_parking);
			TextView nbplaces = (TextView) ligne.findViewById(R.id.nbplaces);
			TextView stationnement = (TextView) ligne.findViewById(R.id.stationnement);
			RatingBar note = (RatingBar) ligne.findViewById(R.id.notation);
			TextView location = (TextView) ligne.findViewById(R.id.distance);
			
			Parking pkg= (Parking) getListAdapter().getItem(position);
			Toast.makeText(ListParking.this, pkg.getNote()+"", Toast.LENGTH_SHORT).show();//à enlever
			nom_parking.setText(pkg.getNameParking());
			nbplaces.setText(pkg.getNbOfPlaces()+"");
			stationnement.setText(pkg.getStationnement());
			note.setRating(pkg.getNote());
			location.setText(pkg.getLocation());
			
			return ligne;
		}

		
		
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.parkings, menu);
		return true;
	}
	
	@Override
	public void onDestroy(){
		db.fermeture();
		super.onDestroy();
	}

}
